package com.topscore.uke.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.WriterException;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.topscore.uke.R;
import com.topscore.uke.mvp.contractors.GetMemberCountContractor;
import com.topscore.uke.mvp.presenters.GetMemberCountPresenter;
import com.topscore.uke.ui.ToolBarCompatActivity;
import com.topscore.uke.utils.UserInfoManager;
import com.topscore.uke.utils.WXNativeShareUtil;
import com.topscore.utillibrary.util.CodeUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MyMembersActivity extends ToolBarCompatActivity implements View.OnClickListener, GetMemberCountContractor.View {

    @Bind(R.id.member_count)
    TextView memberCount;
    @Bind(R.id.my_members_layout)
    RelativeLayout myMembersLayout;
    @Bind(R.id.invite_register)
    RelativeLayout inviteRegister;
    @Bind(R.id.invite_commend)
    RelativeLayout inviteCommend;

    private GetMemberCountContractor.Presenter mMemberCountPresenter;

//    private ShareUtil mShareUtil;

    private WXNativeShareUtil mNativeShareUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        mShareUtil = new ShareUtil((Activity) mContext, SHARE_MEDIA.WEIXIN);
        setContentView(R.layout.activity_my_members);
        ButterKnife.bind(this);
        mMemberCountPresenter = new GetMemberCountPresenter(this);
        initViews();
    }

    private void initViews() {
        setCentralTitle("我的会员");
//        memberCount.setText(String.format(getResources().getString(R.string.member_count), 200));
        myMembersLayout.setOnClickListener(this);
        inviteCommend.setOnClickListener(this);
        inviteRegister.setOnClickListener(this);
        mMemberCountPresenter.getMemberCount();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_members_layout:
                toMemberList();
                break;
            case R.id.invite_register:
                String inviteRgstUrl = "https://wei.topscore.com.cn/wechat-api/"
                        + UserInfoManager.getUserInfo().getBrand()
                        + "/recommend?recommend="
                        + UserInfoManager.getUserInfo().getLoginName();
//                if (mShareUtil==null){
//                    mShareUtil = new ShareUtil((Activity) mContext, SHARE_MEDIA.WEIXIN);
//                }
//                try {
//                    mShareUtil
//                            .setImage(CodeUtils.createCode(mContext, inviteRgstUrl))
//                            .share();
//                } catch (WriterException e) {
//                    e.printStackTrace();
//                }
                if (mNativeShareUtil==null){
                    mNativeShareUtil = new WXNativeShareUtil(mContext, SendMessageToWX.Req.WXSceneSession);
                }
                try {
                    mNativeShareUtil.shareImage(CodeUtils.createCode(mContext,inviteRgstUrl));
                } catch (WriterException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.invite_commend:
                String inviteCmdUrl = "https://wei.topscore.com.cn/wechat-api/" +
                        UserInfoManager.getUserInfo().getBrand() +
                        "/shopping?employeeID=" +
                        UserInfoManager.getUserInfo().getLoginName();
//                if (mShareUtil==null){
//                    mShareUtil = new ShareUtil((Activity) mContext, SHARE_MEDIA.WEIXIN);
//                }
//                try {
//                    mShareUtil.setImage(CodeUtils.createCode(mContext, inviteCmdUrl))
////                            .share();
//                } catch (WriterException e) {
//                    e.printStackTrace();
//                }

                if (mNativeShareUtil==null){
                    mNativeShareUtil=new WXNativeShareUtil(mContext,SendMessageToWX.Req.WXSceneSession);
                }

                try {
                    mNativeShareUtil.shareImage(CodeUtils.createCode(mContext,inviteCmdUrl));
                } catch (WriterException e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void toMemberList() {
        Intent intent = new Intent(this, MyMembersListActivity.class);
        startActivity(intent);
    }

    @Override
    public void onLoading(String loadingMessage) {

    }

    @Override
    public void onLoadFinished() {

    }

    @Override
    public void onFail(int code, String errorMessage) {
        Toast.makeText(mContext, errorMessage, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onGetCountSuccess(int count) {
        memberCount.setText(String.format(getResources().getString(R.string.member_count), count));
    }
}
